package ssh;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;

import com.jcraft.jsch.Channel;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.Session;

public class SSHExecutor {
	/**
	 * 远程主机描述
	 * 
	 * @author Kerbores
	 *
	 */
	public static class HostInfo {

		private String host;

		private int port = 22;

		private String user = "root";

		private String password;

		public HostInfo(String host, String user, String password) {
			super();
			this.host = host;
			this.user = user;
			this.password = password;
		}

		public HostInfo(String host, int port, String password) {
			super();
			this.host = host;
			this.port = port;
			this.password = password;
		}

		public HostInfo(String host, String password) {
			super();
			this.host = host;
			this.password = password;
		}

		public HostInfo() {
			super();
		}

		public HostInfo(String host, int port, String user, String password) {
			super();
			this.host = host;
			this.port = port;
			this.user = user;
			this.password = password;
		}

		public String getHost() {
			return host;
		}

		public void setHost(String host) {
			this.host = host;
		}

		public int getPort() {
			return port;
		}

		public void setPort(int port) {
			this.port = port;
		}

		public String getUser() {
			return user;
		}

		public void setUser(String user) {
			this.user = user;
		}

		public String getPassword() {
			return password;
		}

		public void setPassword(String password) {
			this.password = password;
		}

	}

	/**
	 * 回调接口
	 * 
	 * @author Kerbores
	 *
	 */
	public static interface Callback {
		/**
		 * console 动作回调
		 * 
		 * @param result
		 */
		void onConsoleWrite(String result);
	}

	/**
	 * 回调方式执行命名,建议使用这种方式进行
	 * 
	 * @param info
	 *            主机信息
	 * @param cmd
	 *            命令
	 * @param done
	 *            回调接口
	 */
	public void execute(HostInfo info, String cmd, Callback done) {
		Session session = null;
		Channel channel = null;
		try {
			JSch jsch = new JSch();
			session = jsch.getSession(info.getUser(), info.getHost(), info.getPort());
			session.setPassword(info.getPassword());
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect(30000);

			channel = session.openChannel("shell");
			channel.connect(1000);

			// 获取输入流和输出流
			InputStream instream = channel.getInputStream();
			OutputStream outstream = channel.getOutputStream();

			// 发送需要执行的SHELL命令，需要用\n结尾，表示回车
			String shellCommand = cmd + " \n";
			outstream.write("stty -echo\n".getBytes());
			outstream.flush();
			outstream.write(shellCommand.getBytes());
			outstream.flush();
			outstream.write("exit\n".getBytes());
			outstream.flush();
			BufferedReader reader = new BufferedReader(new InputStreamReader(instream));
			String buf = null;
			while ((buf = reader.readLine()) != null) {
				done.onConsoleWrite(buf);
			}
		} catch (Exception e) {
			throw new RuntimeException(e.getMessage());
		} finally {
			session.disconnect();
			channel.disconnect();
		}
	}

	/**
	 * 执行命令
	 * 
	 * @param info
	 *            主机信息
	 * @param cmd
	 *            命令
	 * @return 执行结果
	 */
	public String execute(HostInfo info, String cmd) {
		final StringBuilder builder = new StringBuilder();
		execute(info, cmd, new Callback() {

			@Override
			public void onConsoleWrite(String result) {
				builder.append(result);
			}
		});

		return builder.toString();
	}

	public static void main(String[] args) {
		SSHExecutor executor = new SSHExecutor();
		executor.execute(new HostInfo("192.168.10.189", 22, "root", "123456"), "cat /home/kerbores/io.log", new Callback() {

			@Override
			public void onConsoleWrite(String result) {
				System.err.println(result);
			}
		});
	}
}
